package fpc.containers;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Rectangle;
import fpc.layouts.ILayout;
import fpc.styles.skins.IPanelSkin;
import fpc.styles.skins.NullPanelSkin;
import fpc.styles.skins.SkinType;
import fpc.styles.Style;
import fpc.utils.ClassFactory;
import fpc.utils.IFactory;
import fpc.utils.Maths;

class Panel< T:ILayout > extends Container<T>
{
	public function new(layout:T = null)
	{
		super(layout);
		border = 10;
		updateStyle();
	}
	
	public var border(getBorder, setBorder):Int;
	function getBorder()
	{
		return _spanLeft;
	}
	function setBorder(value)
	{
		_spanLeft = _spanRight = _spanTop = _spanBottom = value;
		updateSpans();
		return value;
	}
	
	override function arrange()
	{
		super.arrange();
		_activeSkin.setSize(Std.int(_width), Std.int(_height));
	}
	
	var _activeSkin:IPanelSkin;
	
	function applySkin(value:IPanelSkin)
	{
		if (_activeSkin != null)
		{
			_sprite.removeChild(_activeSkin.object);
		}
		
		_activeSkin = value;
		
		if (value != null)
		{
			_sprite.addChildAt(value.object, 0);
			value.setSize(Std.int(_width), Std.int(_height));
		}
	}
	
	var _skinFactory:IFactory<IPanelSkin>;
	
	override function updateStyle()
	{
		var skinFactory = Style.findSkin(this, SKIN_TYPE_PANEL);
		if (skinFactory != _skinFactory)
		{
			_skinFactory = skinFactory;
			applySkin(skinFactory.create());
		}
	}
	
	public static var SKIN_TYPE_PANEL = new SkinType<IPanelSkin>(
		new ClassFactory<IPanelSkin>(NullPanelSkin)
	);
}
